package ch14;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

public class TrafficLight extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        BorderPane mainpane = new BorderPane();
        Lamps lamps = new Lamps();
        mainpane.setCenter(lamps);
        Buttons buttons = new Buttons();
        mainpane.setBottom(buttons);
        Scene scene = new Scene(mainpane, 250, 500);
        primaryStage.setScene(scene);
        primaryStage.show();
        buttons.redbutton.setOnAction(e -> {
            lamps.redlight.setFill(Color.RED);
            lamps.yellowlight.setFill(Color.WHITE);
            lamps.greenlight.setFill(Color.WHITE);
        });
        buttons.yellowbutton.setOnAction(e -> {
            lamps.redlight.setFill(Color.WHITE);
            lamps.yellowlight.setFill(Color.YELLOW);
            lamps.greenlight.setFill(Color.WHITE);
        });
        buttons.greenbutton.setOnAction(e -> {
            lamps.redlight.setFill(Color.WHITE);
            lamps.yellowlight.setFill(Color.WHITE);
            lamps.greenlight.setFill(Color.GREEN);
        });
    }

    class Lamps extends VBox {
        Circle redlight = new Circle(50);
        Circle yellowlight = new Circle(50);
        Circle greenlight = new Circle(50);

        Lamps() {
            setPadding(new Insets(10, 10, 10, 10));
            setSpacing(10);
            getChildren().clear();
            redlight.setFill(Color.WHITE);
            yellowlight.setFill(Color.WHITE);
            greenlight.setFill(Color.WHITE);
            redlight.setStroke(Color.BLACK);
            yellowlight.setStroke(Color.BLACK);
            greenlight.setStroke(Color.BLACK);
            getChildren().addAll(redlight, yellowlight, greenlight);
            setAlignment(Pos.CENTER);
        }
    }

    class Buttons extends HBox {
        RadioButton redbutton = new RadioButton("Red");
        RadioButton yellowbutton = new RadioButton("Yellow");
        RadioButton greenbutton = new RadioButton("Green");
        ToggleGroup buttons = new ToggleGroup();

        Buttons() {
            setPadding(new Insets(10, 10, 10, 10));
            setSpacing(10);
            getChildren().clear();
            redbutton.setToggleGroup(buttons);
            yellowbutton.setToggleGroup(buttons);
            greenbutton.setToggleGroup(buttons);
            getChildren().addAll(redbutton, yellowbutton, greenbutton);
            setAlignment(Pos.CENTER);
        }
    }
}